package com.moive.news.tqnewsapplication.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.base.Adapter.BaseListAdapter;
import com.moive.news.tqnewsapplication.base.BaseActivity;
import com.moive.news.tqnewsapplication.entity.NewsInfo;
import com.moive.news.tqnewsapplication.entity.NewsPinlunInfo;
import com.moive.news.tqnewsapplication.entity.ResourcesManager;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.utils.DateUtil;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.TimeUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.moive.news.tqnewsapplication.weight.MakeListView;
import com.moive.news.tqnewsapplication.weight.mylistview.SecondXListView;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import de.hdodenhof.circleimageview.CircleImageView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import okhttp3.Call;
import onekeyshare.OnekeyShare;

/**
 * Created by Administrator on 2018/1/12.
 */

public class QiushiDetilsActivity extends BaseActivity {
    NewsInfo.RowsBean bean;
    @BindView(R.id.news_back)
    ImageView newsBack;
    @BindView(R.id.newsdetils_title)
    TextView newsdetilsTitle;
    @BindView(R.id.collect_text)
    TextView collectText;
    @BindView(R.id.newsdetils_listview)
    SecondXListView newsdetilsListview;
    @BindView(R.id.news_comment)
    TextView newsComment;
    @BindView(R.id.news_share)
    ImageView newsShare;
    @BindView(R.id.news_dianzan)
    ImageView newsDianzan;
    @BindView(R.id.news_dianzan_number)
    TextView newsDianzanNumber;
    @BindView(R.id.chatContent)
    EditText chatContent;
    @BindView(R.id.voiceLayout)
    ImageView voiceLayout;
    @BindView(R.id.chatSendBtn)
    TextView chatSendBtn;
    @BindView(R.id.inputRelativeLayout)
    RelativeLayout inputRelativeLayout;
    @BindView(R.id.web_realte)
    RelativeLayout webRealte;
    private Gson gson = new Gson();
    private View view;
    List<NewsPinlunInfo.DataBean> list = new ArrayList<>();
    QiushiPinlunAdapter adapter;
    private int type = -1;
    private CircleImageView qiushi_item01_img;
    private TextView qiushi_item01_nichen;
    private TextView qiushi_item01_time;
    private TextView qiushi_item01_desrc;
    private NineGridView ad_mypro_nine;
    private LinearLayout qiushi_item01_fenxiang_ll;
    private LinearLayout qiushi_item01_pinglun_ll;
    private TextView qiushi_item01_pinglun_text;
    private LinearLayout qiushi_item01_dianzan_ll;
    private TextView qiushi_item01_dianzan_text;
    private ImageView mypro_dianzan_img;
    private CircleImageView qiushi_item02_img;
    private TextView qiushi_item02_nichen;
    private TextView qiushi_item02_time;
    private TextView qiushi_item02_desrc;
    private LinearLayout qiushi_item02_fenxiang_ll;
    private LinearLayout qiushi_item02_pinglun_ll;
    private TextView qiushi_item02_pinglun_text;
    private LinearLayout qiushi_item02_dianzan_ll;
    private TextView qiushi_item02_dianzan_text;
    private ImageView qiushi_item02_dianzan_img;
    private CircleImageView qiushi_item03_img;
    private TextView qiushi_item03_nichen;
    private TextView qiushi_item03_time;
    private TextView qiushi_item03_desrc;
    private JCVideoPlayerStandard videoplayer;
    private LinearLayout qiushi_item03_fenxiang_ll;
    private LinearLayout qiushi_item03_pinglun_ll;
    private TextView qiushi_item03_pinglun_text;
    private LinearLayout qiushi_item03_dianzan_ll;
    private TextView qiushi_item03_dianzan_text;
    private ImageView qiushi_item03_dianzan_img;
    private int code = -1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        Intent intent = getIntent();
        if (intent != null) {
            String s = intent.getStringExtra("url");
            bean = gson.fromJson(s, NewsInfo.RowsBean.class);
            if (bean.getMediaType() == 1 || bean.getMediaType() == 2
                    || bean.getMediaType() == 4) {
                view = LayoutInflater.from(QiushiDetilsActivity.this).inflate(R.layout.qiushi_item_01, null);
                qiushi_item01_img = view.findViewById(R.id.qiushi_item01_img);
                ad_mypro_nine = view.findViewById(R.id.ad_mypro_nine);
                qiushi_item01_nichen = view.findViewById(R.id.qiushi_item01_nichen);
                qiushi_item01_time = view.findViewById(R.id.qiushi_item01_time);
                qiushi_item01_desrc = view.findViewById(R.id.qiushi_item01_desrc);
                qiushi_item01_fenxiang_ll = view.findViewById(R.id.qiushi_item01_fenxiang_ll);
                qiushi_item01_pinglun_ll = view.findViewById(R.id.qiushi_item01_pinglun_ll);
                qiushi_item01_pinglun_text = view.findViewById(R.id.qiushi_item01_pinglun_text);
                qiushi_item01_dianzan_ll = view.findViewById(R.id.qiushi_item01_dianzan_ll);
                qiushi_item01_dianzan_text = view.findViewById(R.id.qiushi_item01_dianzan_text);
                mypro_dianzan_img = view.findViewById(R.id.mypro_dianzan_img);
                Glide.with(QiushiDetilsActivity.this)
                        .load(bean.getUserHeadImg())
                        .centerCrop()
                        .dontAnimate()
                        .placeholder(R.drawable.icon)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(qiushi_item01_img);
                qiushi_item01_nichen.setText(bean.getUserNickName());
                String newtime = DateUtil.getDescriptionTimeFromTimestamp(bean.getTime());
                qiushi_item01_time.setText(newtime);
                try {
                    String comment = URLDecoder.decode(bean.getContent(), "UTF-8");
                    qiushi_item01_desrc.setText(comment);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                qiushi_item01_pinglun_text.setText(bean.getCommentCount() + "");
                qiushi_item01_dianzan_text.setText(bean.getLikeCount() + "");
                ArrayList<ImageInfo> imageInfo = new ArrayList<>();
                List<NewsInfo.RowsBean.ImageInfo> list = bean.getImages();
                for (NewsInfo.RowsBean.ImageInfo imagesBean : list) {
                    ImageInfo info = new ImageInfo();
                    info.setThumbnailUrl(imagesBean.getThumbUrl());
                    info.setBigImageUrl(imagesBean.getLargeUrl());
                    imageInfo.add(info);
                }
                ad_mypro_nine.setAdapter(new NineGridViewClickAdapter(QiushiDetilsActivity.this, imageInfo));
                qiushi_item01_fenxiang_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onOneKeyShare();
                    }
                });
                qiushi_item01_pinglun_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (inputRelativeLayout.getVisibility() == View.GONE) {
                            inputRelativeLayout.setVisibility(View.VISIBLE);
                            type = 0;
                        }
                    }
                });
                qiushi_item01_dianzan_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        code = 0;
                        dianzan(code);
                    }
                });


            } else if (bean.getMediaType() == 0) {
                view = LayoutInflater.from(QiushiDetilsActivity.this).inflate(R.layout.qiushi_item_02, null);
                qiushi_item02_img = view.findViewById(R.id.qiushi_item02_img);
                qiushi_item02_nichen = view.findViewById(R.id.qiushi_item02_nichen);
                qiushi_item02_time = view.findViewById(R.id.qiushi_item02_time);
                qiushi_item02_pinglun_ll = view.findViewById(R.id.qiushi_item02_pinglun_ll);
                qiushi_item02_pinglun_text = view.findViewById(R.id.qiushi_item02_pinglun_text);
                qiushi_item02_desrc = view.findViewById(R.id.qiushi_item02_desrc);
                qiushi_item02_dianzan_img = view.findViewById(R.id.qiushi_item02_dianzan_img);
                qiushi_item02_dianzan_text = view.findViewById(R.id.qiushi_item02_dianzan_text);
                qiushi_item02_fenxiang_ll = view.findViewById(R.id.qiushi_item02_fenxiang_ll);
                qiushi_item02_dianzan_ll = view.findViewById(R.id.qiushi_item02_dianzan_ll);


                Glide.with(QiushiDetilsActivity.this)
                        .load(bean.getUserHeadImg())
                        .centerCrop()
                        .dontAnimate()
                        .placeholder(R.drawable.icon)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(qiushi_item02_img);

                qiushi_item02_nichen.setText(bean.getUserNickName());
                String newtime1 = DateUtil.getDescriptionTimeFromTimestamp(bean.getTime());
                qiushi_item02_time.setText(newtime1);
                try {
                    if (!TextUtils.isEmpty(bean.getContent())) {
                        String comment = URLDecoder.decode(bean.getContent(), "UTF-8");
                        qiushi_item02_desrc.setText(comment);
                    }
//                    qiushi_item02_desrc.setText("");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                qiushi_item02_pinglun_text.setText(bean.getCommentCount() + "");
                qiushi_item02_dianzan_text.setText(bean.getLikeCount() + "");


                qiushi_item02_fenxiang_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        onOneKeyShare();
                    }
                });
                qiushi_item02_pinglun_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (inputRelativeLayout.getVisibility() == View.GONE) {
                            inputRelativeLayout.setVisibility(View.VISIBLE);
                            type = 0;
                        }
                    }
                });
                qiushi_item02_dianzan_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        code = 1;
                        dianzan(code);
                    }
                });
            } else {
                view = LayoutInflater.from(QiushiDetilsActivity.this).inflate(R.layout.qiushi_item_03, null);
                qiushi_item03_img = view.findViewById(R.id.qiushi_item03_img);
                qiushi_item03_nichen = view.findViewById(R.id.qiushi_item03_nichen);
                qiushi_item03_time = view.findViewById(R.id.qiushi_item03_time);
                qiushi_item03_desrc = view.findViewById(R.id.qiushi_item03_desrc);
                videoplayer = view.findViewById(R.id.videoplayer);
                qiushi_item03_fenxiang_ll = view.findViewById(R.id.qiushi_item03_fenxiang_ll);
                qiushi_item03_pinglun_ll = view.findViewById(R.id.qiushi_item03_pinglun_ll);
                qiushi_item03_pinglun_text = view.findViewById(R.id.qiushi_item03_pinglun_text);
                qiushi_item03_dianzan_ll = view.findViewById(R.id.qiushi_item03_dianzan_ll);
                qiushi_item03_dianzan_text = view.findViewById(R.id.qiushi_item03_dianzan_text);
                qiushi_item03_dianzan_img = view.findViewById(R.id.qiushi_item03_dianzan_img);

                qiushi_item03_nichen.setText(bean.getUserNickName());
                Glide.with(QiushiDetilsActivity.this)
                        .load(bean.getUserHeadImg())
                        .centerCrop()
                        .dontAnimate()
                        .placeholder(R.drawable.icon)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(qiushi_item03_img);
                String time = DateUtil.getDescriptionTimeFromTimestamp(bean.getTime());
                qiushi_item03_time.setText(time);
                try {
                    String comment = URLDecoder.decode(bean.getContent(), "UTF-8");
                    qiushi_item03_desrc.setText(comment);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                qiushi_item03_pinglun_text.setText(bean.getCommentCount() + "");
                qiushi_item03_dianzan_text.setText(bean.getLikeCount() + "");
                boolean setUp = videoplayer.setUp(bean.getMp4Url(),
                        JCVideoPlayer.SCREEN_LAYOUT_LIST, "");
                if (setUp) {
                    Glide.with(QiushiDetilsActivity.this).load(bean.getMediumCover()).into(videoplayer.thumbImageView);
                }
                qiushi_item03_pinglun_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (inputRelativeLayout.getVisibility() == View.GONE) {
                            inputRelativeLayout.setVisibility(View.VISIBLE);
                            type = 0;
                        }
                    }
                });
                qiushi_item03_dianzan_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        code = 2;
                        dianzan(code);
                    }
                });
                qiushi_item03_fenxiang_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onOneKeyShare();
                    }
                });

            }
        }
        webRealte.setVisibility(View.GONE);
        collectText.setVisibility(View.GONE);
        adapter = new QiushiPinlunAdapter(QiushiDetilsActivity.this);
        newsdetilsListview.setPullRefreshEnable(false);
        newsdetilsListview.setAdapter(adapter);
        newsdetilsListview.addHeaderView(view);
        chatContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().length() == 28) {
                    ToastUtils.LongToast("你最多发送评论不超过28个字！");
                }
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        chatContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {//EditorInfo.IME_ACTION_SEARCH、EditorInfo.IME_ACTION_SEND等分别对应EditText的imeOptions属性
                    //TODO回车键按下时要执行的操作
                }
                if (TextUtils.isEmpty(PreferenceUtil.getString("token", ""))) {
                    ToastUtils.ShortToast("你还未登录，无法评论！");
                    return false;
                }
                //发表父评论
                if (type == 0) {
                    comment(chatContent.getText().toString(), bean.getId(), "", "");
                } else {
//                    //发表子评论
//                    if (!TextUtils.isEmpty(id)) {
//                        comment(chatContent.getText().toString(), "", parent_id, id);
//                    }
                }
                return false;
            }
        });
        getdata();
    }

    //评论接口commit 评价内容 emotionID动态id ，parentCommentID父评论id,toUserId 发给某人id
    private void comment(String commit, String emotionID, String parentCommentID, String toUserId) {
        String token = PreferenceUtil.getString("token", "");
        OkHttpUtils.post()
                .url(BaseApi.NEWS_COMMENT)
                .addParams("token", token)
                .addParams("newsId", emotionID)
                .addParams("content", commit)
                .addParams("parentId", parentCommentID)
                .addParams("toUserId", toUserId)
                .build().execute(new OkManagerCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG, e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.optInt("code") == 0) {
                        ToastUtils.ShortToast("评论成功！");
                        list.clear();
                        getdata();
                        hideSoftKeyboard();
                        if (inputRelativeLayout.getVisibility() == View.VISIBLE) {
                            inputRelativeLayout.setVisibility(View.GONE);
                            chatContent.setText("");
                            type = 0;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //获取所有新闻评论
    private void getdata() {
        OkHttpUtils.get()
                .url(BaseApi.NEWS_LOOK_COMMENT)
                .addParams("newsId", bean.getId())
                .build().execute(new OkManagerCallback(this) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG, e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getInt("code") == 0) {
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        if (jsonArray != null) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
                                NewsPinlunInfo.DataBean dataBean1 = gson.fromJson(jsonObject1 + "", NewsPinlunInfo.DataBean.class);
                                list.add(dataBean1);
                            }
                            adapter.addItems(list);
                            if (type == 1) {
                                adapter.notifyDataSetChanged();
                                type = -1;
                            } else if (type == 0) {
                                adapter.notifyDataSetChanged();
                                type = -1;
                            }
                        }
                        if (jsonArray.length() == 0) {
                            ToastUtils.ShortToast("没有更多数据了！");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @OnClick({R.id.news_back, R.id.chatSendBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.news_back:
                finish();
                break;
            case R.id.news_share:
                onOneKeyShare();
                break;
            case R.id.news_dianzan:
//                dianzan();
                break;
            case R.id.chatSendBtn:
                if (TextUtils.isEmpty(PreferenceUtil.getString("token", ""))) {
                    ToastUtils.ShortToast("你还未登录，无法评论！");
                    return;
                }
                //发表父评论
                if (type == 0) {
                    comment(chatContent.getText().toString(), bean.getId(), "", "");
                } else {
//                    //发表子评论
//                    if (!TextUtils.isEmpty(id)) {
//                        comment(chatContent.getText().toString(), "", parent_id, id);
//                    }
                }
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        JCVideoPlayer.releaseAllVideos();
    }

    //点赞
    private void dianzan(int code) {
        if (TextUtils.isEmpty(PreferenceUtil.getString("token", ""))) {
            ToastUtils.ShortToast("你还未登录，请前往登录");
            return;
        }
        OkHttpUtils.get()
                .url(BaseApi.NEWS_DIANZAN)
                .addParams("token", PreferenceUtil.getString("token", ""))
                .addParams("newsId", bean.getId())
                .build().execute(new OkManagerCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG, e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getInt("code") == 0) {
                        int likes = bean.getLikeCount() + 1;
                        if (code == 0) {
                            qiushi_item01_dianzan_text.setText(likes + "");
                            mypro_dianzan_img.setBackgroundResource(R.drawable.icon_dianzan02);
                        } else if (code == 1) {
                            qiushi_item02_dianzan_text.setText(likes + "");
                            qiushi_item02_dianzan_img.setBackgroundResource(R.drawable.icon_dianzan02);
                        } else if (code == 2) {
                            qiushi_item03_dianzan_text.setText(likes + "");
                            qiushi_item03_dianzan_img.setBackgroundResource(R.drawable.icon_dianzan02);
                        }
                    } else {
                        if (code == 0) {
                            mypro_dianzan_img.setBackgroundResource(R.drawable.icon_dianzan02);
                        } else if (code == 1) {
                            qiushi_item02_dianzan_img.setBackgroundResource(R.drawable.icon_dianzan02);
                        } else {
                            qiushi_item03_dianzan_img.setBackgroundResource(R.drawable.icon_dianzan02);
                        }
                        ToastUtils.ShortToast("你已经点过赞了");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //分享
    private void onOneKeyShare() {
        OnekeyShare oks = new OnekeyShare();
        oks.setText(bean.getContent());
        oks.setTitle("爱小道分享");
        oks.setTitleUrl("www.tqiant.com");
//        oks.setImageUrl(inputurl);
        oks.setSilent(true);
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                String msg = ResourcesManager.actionToString(i);
                String text = platform.getName() + " completed at " + msg;
                ToastUtils.ShortToast("分享成功！");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                String msg = ResourcesManager.actionToString(i);
                String text = platform.getName() + "caught error at " + msg;
                ToastUtils.ShortToast("分享失败！");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                String msg = ResourcesManager.actionToString(i);
                String text = platform.getName() + " canceled at " + msg;
//                ToastUtils.ShortToast(text);
            }
        });
        Bitmap logo = BitmapFactory.decodeResource(QiushiDetilsActivity.this.getResources(), R.mipmap.ic_launcher);
        String label = QiushiDetilsActivity.this.getString(R.string.app_name);
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                String text = "Customer Logo -- ShareSDK " + ShareSDK.getSDKVersionName();
                Toast.makeText(QiushiDetilsActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        };
        oks.setCustomerLogo(logo, label, listener);
        oks.show(QiushiDetilsActivity.this);
    }

    public class QiushiPinlunAdapter extends BaseListAdapter<NewsPinlunInfo.DataBean> {
        private Context context;

        public QiushiPinlunAdapter(Context context) {
            this.context = context;
        }

        @Override
        protected View getItemView(int position, View view, ViewGroup parent, List<NewsPinlunInfo.DataBean> item) {
            ViewHolder viewHolder = null;
            if (viewHolder == null) {
                viewHolder = new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.newspinlun_item, null);
                viewHolder.newspinglun_img = view.findViewById(R.id.newspinglun_img);
                viewHolder.newspinglun_nichen = view.findViewById(R.id.newspinglun_nichen);
                viewHolder.newspinglun_time = view.findViewById(R.id.newspinglun_time);
                viewHolder.newspinglun_desrc = view.findViewById(R.id.newspinglun_desrc);
                viewHolder.newsdteils_number = view.findViewById(R.id.newsdteils_number);
                viewHolder.newsdetils_dianzan_img = view.findViewById(R.id.newsdetils_dianzan_img);
                viewHolder.makelistview = view.findViewById(R.id.makelistview);
                view.setTag(this);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            Glide.with(context)
                    .load(item.get(position).getFromUserHeadImg())
                    .centerCrop()
                    .dontAnimate()
                    .placeholder(R.drawable.icon)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(viewHolder.newspinglun_img);
            viewHolder.newspinglun_nichen.setText(item.get(position).getFromUserNickName());
            String time = TimeUtil.parseDateTime(item.get(position).getCreateDate());
            viewHolder.newspinglun_time.setText(time);
            String comment = null;
            try {
                comment = URLDecoder.decode(item.get(position).getContent(), "UTF-8");
                viewHolder.newspinglun_desrc.setText(comment);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return view;
        }

        class ViewHolder {
            private CircleImageView newspinglun_img;
            private TextView newspinglun_nichen;
            private TextView newspinglun_time;
            private TextView newspinglun_desrc;
            private TextView newsdteils_number;
            private ImageView newsdetils_dianzan_img;
            private MakeListView makelistview;
        }
    }
}
